import axios from "axios"
import config from "../../config"
import {Message} from "element-ui"

const instance = axios.create({
  baseURL: config.baseUrl,
  timeout: config.timeout,
})

instance.interceptors.response.use(value => {
  // 正常接受到了200响应
  const { data } = value
  const {code, msg } = data
  if (code !== 0) {
    Message.error(msg)
    const err = new Error()
    err.data = data
    throw err
  }
  return data
}, error => {
  Message.error(error.message)
})

// /user?id=3
export default {
  get: (path, params) => {
    return instance.get(path, {params})
  },
  post: (path, data) => {
    return instance.post(path, data)
  }
}